
#include "tiledsprite.h"	

#include <assert.h>

void TiledSprite_Init (TiledSprite* tiledSprite, const char filename[],int width,int height,int tileWidth,int tileHeight)
{
	assert((width > 0) && (height > 0) && (tileWidth >= width) && (tileHeight >= height));
	tiledSprite->textureId = GraphicsCore_LoadTexture(filename,1);
	tiledSprite->width = width;
	tiledSprite->height = height;
	tiledSprite->tileWidth = tileWidth;
	tiledSprite->tileHeight = tileHeight;
}

void TiledSprite_GetSprite(const TiledSprite* tiledSprite, Sprite* sprite, const Vector3f* position, const Color* color, float theta, float phi, int index)
{
	int width = tiledSprite->tileWidth / tiledSprite->width;
	int	height = tiledSprite->tileHeight / tiledSprite->height;
	float x = index % height;
	float y = index / height;
	assert(tiledSprite->textureId != 0);
	Sprite_InitWithTextureCoord(sprite,tiledSprite->textureId,(float)(x * tiledSprite->width)/tiledSprite->tileWidth,(float)(y * tiledSprite->height)/tiledSprite->tileHeight,(float)tiledSprite->width/tiledSprite->tileWidth,(float)tiledSprite->height/tiledSprite->tileHeight,position,color,theta,phi);
}

void TiledSprite_Destroy(TiledSprite* tiledSprite)
{
	if (tiledSprite->textureId != 0)
		Texture_Destroy(tiledSprite->textureId);
	tiledSprite->textureId = 0;
	tiledSprite->height = 0;
	tiledSprite->width = 0;
	tiledSprite->tileHeight = 0;
	tiledSprite->tileWidth = 0;
}